package com.blog.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.blog.pojo.Comment;
import com.blog.pojo.PageBean;
import com.blog.service.CommentService;
import com.blog.util.UtilResponse;

@Controller
@RequestMapping("/admin/comment")
public class CommentAdminController {
	
	@Resource
	private CommentService commentService;
	
	/**
	 * 删除评论
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/delete")
	public String delete(String ids,HttpServletResponse response) throws Exception{
		String idsStr[] = ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			commentService.delete(Integer.parseInt(idsStr[i]));
		}
		//返回结果
		JSONObject result = new JSONObject();
		result.put("success", true);
		UtilResponse.write(response, result);
		return null;
	}
	
	/**
	 * 分页查询评论
	 * @param page
	 * @param rows
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(int page,int rows,HttpServletResponse response) throws Exception{
		//分页bean
		PageBean bean = new PageBean(page, rows);
		//查询条件
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", bean.getStart());
		map.put("size", bean.getPageSize());
		//查询数据
		List<Comment> list = commentService.list(map);
		Long total = commentService.getTotal(map);
		JSONObject result = new JSONObject();
		result.put("rows", list);
		result.put("total", total);
		UtilResponse.write(response, result);
		return null;
	}
	
}
